package com.zyf.wjy.controller;

import com.zyf.wjy.common.Result;
import com.zyf.wjy.entity.FarmingRecord;
import com.zyf.wjy.service.CropService;
import com.zyf.wjy.service.FarmingRecordService;
import com.zyf.wjy.service.FieldService;
import com.zyf.wjy.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 农事记录控制器
 */
@Controller
@RequestMapping("/farming")
public class FarmingRecordController {
    
    @Autowired
    private FarmingRecordService farmingRecordService;
    
    @Autowired
    private FieldService fieldService;
    
    @Autowired
    private CropService cropService;
    
    @Autowired
    private MaterialService materialService;
    
    /**
     * 农事记录列表页面
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<FarmingRecord> records = farmingRecordService.findAll();
        model.addAttribute("records", records);
        model.addAttribute("fields", fieldService.findAll());
        model.addAttribute("crops", cropService.findAll());
        model.addAttribute("materials", materialService.findAll());
        return "farming/list";
    }
    
    /**
     * 获取记录详情
     */
    @GetMapping("/detail/{id}")
    @ResponseBody
    public Result<FarmingRecord> detail(@PathVariable Long id) {
        FarmingRecord record = farmingRecordService.findById(id);
        if (record != null) {
            return Result.success(record);
        } else {
            return Result.error("记录不存在");
        }
    }
    
    /**
     * 新增/编辑页面
     */
    @GetMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, Model model) {
        if (id != null) {
            FarmingRecord record = farmingRecordService.findById(id);
            model.addAttribute("record", record);
        }
        model.addAttribute("fields", fieldService.findAll());
        model.addAttribute("crops", cropService.findAll());
        model.addAttribute("materials", materialService.findAll());
        return "farming/edit";
    }
    
    /**
     * 保存农事记录
     */
    @PostMapping("/save")
    @ResponseBody
    public Result<FarmingRecord> save(@RequestBody FarmingRecord record) {
        try {
            FarmingRecord saved = farmingRecordService.save(record);
            return Result.success("保存成功", saved);
        } catch (Exception e) {
            return Result.error("保存失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除农事记录
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Result<Void> delete(@PathVariable Long id) {
        try {
            boolean success = farmingRecordService.deleteById(id);
            if (success) {
                return Result.success("删除成功", null);
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }
}

